package com.moleq.dialog;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.mgpos.activities.R;
import com.moleq.models.Receipt;

public class ReceiptEditDialog extends Dialog implements OnResultListener {

	private OnResultListener mOnResultListener;
	private Button btn_header_ok,btn_header_cancel;
	private EditText et_header;
	private LinearLayout ll_dialog_perview_mode_layout;
	private Context mContext;
	private List<Receipt> listReceipt;
	private Receipt mReceipt;
	private MessageDialog mMessageDialog;
	private String action = "";
	private int type;
	
	public void onCloseWithReturn(int what,Object obj){
		mOnResultListener.onReturnValue(what,obj);
		dismiss();
	}

	public ReceiptEditDialog(Context context, Object base) {
		super(context,R.style.dialog2);
		constructor(context,base);
	}
	
	public void setSource(List<Receipt> listReceipt,Receipt receipt,String action,int type){
		this.listReceipt = listReceipt;
		this.mReceipt = receipt;
		this.action = action;
		this.type = type;
	}
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}

	public ReceiptEditDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.ps_dialog_edit_receipt);
		this.setCancelable(false);
	
		ll_dialog_perview_mode_layout = (LinearLayout) findViewById(R.id.ll_dialog_perview_mode_layout);
		et_header = (EditText) findViewById(R.id.et_header);
		
		btn_header_ok = (Button) findViewById(R.id.btn_header_ok);
		btn_header_cancel = (Button) findViewById(R.id.btn_header_cancel);
		ll_dialog_perview_mode_layout.setOnClickListener(OnClickDialogWindowListener);
		btn_header_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_header_cancel.setOnClickListener(OnClickDialogWindowListener);
	}
	
	public void show(){
		super.show();	
		getDetail(mReceipt);
		et_header.requestFocus();
	}
	
	public void getDetail(Receipt header){
		et_header.setText(header.getMsg());
	}
	
	public Button.OnClickListener OnClickDialogWindowListener = new Button.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_header_ok:
				String ls_value = et_header.getText().toString();
				String ls_size = mReceipt.getLarge();
				if (ls_size.equals("L") && ls_value.length() > 23) {
					ShowMessage(
							"No more than 23 large font size characters.",
							mMessageDialog.MESSAGE_BOX_OK,
							mMessageDialog.ICON_INFO);
					return;
				}
				
				mReceipt.setMsg(ls_value);
				if (mReceipt.getType().equals("I") && action.equals("add")) {
					listReceipt.add(mReceipt);
				}
				mReceipt.setEdit(true);
				
				onCloseWithReturn(type, listReceipt);
				dismiss();
				break;
			case R.id.btn_header_cancel:
				dismiss();
				break;
			case R.id.ll_dialog_perview_mode_layout:
				InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
				inputMethodManager.hideSoftInputFromWindow(ReceiptEditDialog.this
						.getCurrentFocus().getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
				break;
			}
		}
	};
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			
		}

		public void onClickNo() {
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			mMessageDialog.dismiss();
		}
	};
	
	public void ShowMessage(String text,int type,int icon){
		mMessageDialog = new MessageDialog(mContext);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass,
			int viewId, String returnvalue) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub
		
	}
}
